#include "../include/Data.hpp"
#include "../include/Texture.hpp"
#include "../include/external/GLFT_Font.hpp"
#include "../include/SoundEngine.hpp"
using namespace klas;

#include <iostream>
using namespace std;

iReal getType(string file);

Data::Data()
{
	m_data.clear();
  m_invalidData = new InvalidData();
}

Data::~Data()
{
  for(map<string, DataObject*>::iterator it = m_data.begin(); it != m_data.end(); ++it)
  {
    it->second->kill();
    delete((it->second));
  }

	m_data.clear();
  if(m_invalidData)
  {
    delete(m_invalidData);
    m_invalidData = 0;
  }
}

bool Data::load(string key, string file, iReal type)
{
	// We don't want to reuse a key.
	if(has(key))
		return false;
	
	// If we don't know what type of file to load, so fetch it from the file.
  if(type == INVALID_DATA)
	{
		type = getType(file);
	}
	
	// Create our storage
  DataObject* obj;
		
	// Based on our type, load in the data.
	switch(type)
	{		
		case TEXTURE:
    {
      obj = new Texture(file);
      m_data[key] = obj;
			return true;
    }

    case FONT:
    {
      iReal fontSize = 18;
      obj = new Font(file, fontSize);
      m_data[key] = obj;
      return true;
    }

    case MUSIC:
    {
      obj = new Music(file);
      m_data[key] = obj;
      return true;
    }

    case SOUND_EFFECT:
    {
      obj = new SoundEffect(file);
      m_data[key] = obj;
      return true;
    }
	}
	
	return false;
}

DataObject* Data::get(string key)
{
	if(has(key))
	{
    return m_data[key];
	}
	
  return m_invalidData;
}

bool Data::has(string key)
{
	if(m_data.find(key) == m_data.end())
		return false;
	return true;
}

bool contains(string what, string file)
{
	return file.find(what) != string::npos;
}

iReal getType(string file)
{
	if(contains(".png", file) || contains(".bmp", file) || contains(".jpg", file)
			|| contains(".tga", file))
		return Data::TEXTURE;
		
  if(contains(".ttf", file) || contains(".otf", file))
		return Data::FONT;
}









DataObject::~DataObject()
{
}
/* InvalidData definition */


InvalidData::InvalidData()
{
  m_isValid = false;
}

InvalidData::~InvalidData()
{
}

bool InvalidData::isValid() const
{
  return false;
}

iReal InvalidData::getObjectType() const
{
  return Data::INVALID_DATA;
}

bool InvalidData::kill()
{
  return false;
}
